package com.tim.app.ui.fragment.person;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.ClipboardManager;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.application.library.base.BaseFragment;
import com.application.library.net.StringResponseCallback;
import com.google.gson.Gson;
import com.tim.app.R;
import com.tim.app.server.api.API_User;
import com.tim.app.server.logic.UserManager;
import com.tim.app.server.result.ResponseResult;
import com.tim.app.server.result.ShareInfoBean;
import com.tim.app.share.UMShareUtil;
import com.tim.app.util.ToastUtil;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

/**
 * Created by wdy on 2017/8/21.
 */

public class InvateFriendFragment extends BaseFragment {
    private final String TAG = InvateFriendFragment.class.getSimpleName();
    private View rootView;
    private ImageView iv_statement_invatefriend;
    private LinearLayout ll_invate_wx, ll_invate_qq, ll_invate_wb, ll_invate_quan;
    private TextView tv_copy_invatecode;//複製邀請碼
    private TextView tv_invate_code, tv_invate, tv_copy_invate_code;//邀請碼

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_invatefriend, container, false);
            initViews(rootView);
            initEvent();
        }
        return rootView;
    }

    UMShareUtil shareUtil;

    private void initEvent() {
        shareUtil = new UMShareUtil(getActivity(), new UMShareListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {
                showLoadingDialog();
            }

            @Override
            public void onResult(SHARE_MEDIA share_media) {
                hideLoadingDialog();
            }

            @Override
            public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                hideLoadingDialog();
                ToastUtil.showToast("分享失败");
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media) {
                hideLoadingDialog();
                ToastUtil.showToast("分享取消");
            }
        });
        ll_invate_quan.setOnClickListener(OnClickListener);
        ll_invate_wx.setOnClickListener(OnClickListener);
        ll_invate_qq.setOnClickListener(OnClickListener);
        ll_invate_wb.setOnClickListener(OnClickListener);
        iv_statement_invatefriend.setOnClickListener(OnClickListener);
        tv_copy_invatecode.setOnClickListener(OnClickListener);
        String uuid = getString(R.string.invate_code, UserManager.ins().getLoginUser().getUuid());
        tv_invate_code.setText(uuid);
        getShareURL();
    }

    private ShareInfoBean shareInfoBean;

    private void getShareURL() {
        API_User.ins().getShareURL(TAG, UserManager.ins().getLoginUser().getUuid(), new StringResponseCallback() {
            @Override
            public boolean onStringResponse(String resultValue, int errCode, String errMsg, int id, boolean formCache) {
                Log.e("tag", "resultValue====" + resultValue);
                if (errCode == ResponseResult.ResultStatus.OK && resultValue != null) {
                    shareInfoBean = new Gson().fromJson(resultValue, ShareInfoBean.class);
                }
                return false;
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        hideLoadingDialog();
    }

    private View.OnClickListener OnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.iv_statement_invatefriend:
                    break;
                case R.id.ll_invate_wx:
                    shareUtil.shareLink(SHARE_MEDIA.WEIXIN, getString(R.string.share_tittle), getString(R.string.share_content), shareInfoBean.getRes().getHashmap().getUrl());
                    break;
                case R.id.ll_invate_qq:
                    shareUtil.shareLink(SHARE_MEDIA.QQ, getString(R.string.share_tittle), getString(R.string.share_content), shareInfoBean.getRes().getHashmap().getUrl());
                    break;
                case R.id.ll_invate_wb:
                    shareUtil.shareLink(SHARE_MEDIA.SINA, getString(R.string.share_tittle), getString(R.string.share_content), shareInfoBean.getRes().getHashmap().getUrl());
                    break;
                case R.id.ll_invate_quan:
                    shareUtil.shareLink(SHARE_MEDIA.WEIXIN_CIRCLE, getString(R.string.share_tittle), "", shareInfoBean.getRes().getHashmap().getUrl());
                    break;
                case R.id.tv_copy_invatecode:
                    ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
                    cm.setText(UserManager.ins().getLoginUser().getUuid());
                    ToastUtil.showToast("复制成功");
                    break;

            }
        }
    };

    private void initViews(View rootView) {
        tv_copy_invate_code = (TextView) rootView.findViewById(R.id.tv_copy_invate_code);
        tv_invate = (TextView) rootView.findViewById(R.id.tv_invate);
        ll_invate_quan = (LinearLayout) rootView.findViewById(R.id.ll_invate_quan);
        iv_statement_invatefriend = (ImageView) rootView.findViewById(R.id.iv_statement_invatefriend);
        ll_invate_wx = (LinearLayout) rootView.findViewById(R.id.ll_invate_wx);
        ll_invate_qq = (LinearLayout) rootView.findViewById(R.id.ll_invate_qq);
        ll_invate_wb = (LinearLayout) rootView.findViewById(R.id.ll_invate_wb);
        tv_copy_invatecode = (TextView) rootView.findViewById(R.id.tv_copy_invatecode);
        tv_invate_code = (TextView) rootView.findViewById(R.id.tv_invate_code);
//        SpannableString msp = new SpannableString(getString(R.string.invate));
//        msp.setSpan(new ForegroundColorSpan(Color.RED), 15, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        tv_invate.setText(msp);
//        SpannableString copy_code = new SpannableString(getString(R.string.copy_invate_code_statement));
//        copy_code.setSpan(new ForegroundColorSpan(Color.RED), 19, 28, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        tv_copy_invate_code.setText(copy_code);

    }
}
